package com.yunshu.yunshuapp;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.yunshu.yunshuapp.utils.YSUtils;

public class ImageActivity extends AppCompatActivity implements View.OnTouchListener {

    private ImageView mImageView;
    private ImageLoader mImageLoader;
    private DisplayImageOptions options;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);

        NavigationBar bar = (NavigationBar) findViewById(R.id.navigationBar);
        bar.setTitle("图片详情");
        bar.putInActivity(this);

        int screenWidth = YSUtils.screenWidth(this); // 获取屏幕宽度
        mImageView = (ImageView) findViewById(R.id.imageView4);

        Bundle bundle = getIntent().getExtras();
        String  url = (String) bundle.get("imgUrl");

        // 使用ImageLoader之前初始化
        YSUtils.initImageLoader(this);

        // 获取图片加载实例
        mImageLoader = ImageLoader.getInstance();
        options = YSUtils.displayImageOptions();
        mImageLoader.displayImage(url, mImageView);
        mImageLoader.displayImage(url, mImageView, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String s, View view) {

            }

            @Override
            public void onLoadingFailed(String s, View view, FailReason failReason) {

            }

            @Override
            public void onLoadingComplete(String s, View view, Bitmap bitmap) {
//                int screenWidth = YSUtils.screenWidth(ImageActivity.this); // 获取屏幕宽度
//                ViewGroup.LayoutParams layout = mImageView.getLayoutParams();
//                int height = bitmap.getHeight();
//                int width = bitmap.getWidth();
//                layout.width = screenWidth;
//                layout.height = screenWidth/width * height;
//                view.setLayoutParams(layout);
            }

            @Override
            public void onLoadingCancelled(String s, View view) {

            }
        });


    }
    private int mMode;    //模式：1-拉动；2-缩放
    final private int DRAGMODE = 1;
    final private int ZOOMMODE = 2;

    private PointF mStarPt = new PointF();
    private float    mStarDis;
    private PointF  mMidPt = new PointF();

    private Matrix mMatrix = new Matrix();
    private Matrix mCurrMatrix = new Matrix();

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        switch(event.getAction() & MotionEvent.ACTION_MASK)
        {
            //单指按下
            case MotionEvent.ACTION_DOWN:
            {
                mMode = DRAGMODE;
                mStarPt.set(event.getX(), event.getY());
                mCurrMatrix = mImageView.getImageMatrix();

                break;
            }
            //多指缩放的按下
            case MotionEvent.ACTION_POINTER_DOWN:
            {
                mMode = ZOOMMODE;
                mStarDis = GetDistance(event);
                mMidPt  = GetMidPt(event);
                mCurrMatrix = mImageView.getImageMatrix();
                break;
            }
            //移动
            case MotionEvent.ACTION_MOVE:
            {
                if(mMode == DRAGMODE)
                {
                    float left = 0, top = 0, right = 0, bottom = 0;
                    float dx = event.getX() - mStarPt.x;
                    float dy = event.getY() - mStarPt.y;

                    mMatrix.set(mCurrMatrix);
                    mMatrix.postTranslate(dx, dy);
                    mStarPt.set(event.getX(), event.getY());
                }
                else if(mMode == ZOOMMODE)
                {
                    float endDis = GetDistance(event);
                    if (endDis > 10f)
                    {
                        float fScale = 2;// endDis /mStarDis;
                        mMatrix.set(mCurrMatrix);

                        mMatrix.postScale(fScale, fScale, mMidPt.x, mMidPt.y);
                        //注意加上这一句，否则缩放为数量级了
                        mStarDis = endDis;
                    }
                }
                mImageView.setImageMatrix(mMatrix);
                break;
            }
            //单指抬起
            case MotionEvent.ACTION_UP:
                //多指抬起
            case MotionEvent.ACTION_POINTER_UP:
            {
                mMode = 0;
                break;
            }

        }

        return true;
    }

    private PointF GetMidPt(MotionEvent event) {
        PointF point = new PointF();
        float x = event.getX(0) + event.getX(1);
        float y = event.getY(0) + event.getY(1);
        point.set(x / 2, y / 2);
        return point;
    }

    private float GetDistance(MotionEvent event) {
        float x = event.getX(0)-event.getY(1);
        float y = event.getY(0)-event.getY(1);
        return (float)Math.sqrt(x*x+y*y);
    }
}
